#! python3
# webbrowser：是 Python 自带的，打开浏览器获取指定页面

import webbrowser
from urllib import parse

import bs4
import requests

# 输入查找的主题，就能让计算机自动打开浏览器，并在新的选项卡中打开前几项查询结果
# 这个地址有问题，后续解析暂未实现
BAIDU_SEARCH_URL = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd={0}&fenlei=256&oq={1}&rsv_pq=8c2124ad0074ee37&rsv_t=e01czvoDW99AvvyKJbI6%2F8RPmkiiuVdta2rzOwCzPgQRYucadDu6ktO9KAM&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_btype=t"


def baiduSearch(msg):

    try:
        # # 拼接URL 由于输入内容可能包含有中文 需要调用parse.quote函数进行URLEncode
        # url = BAIDU_SEARCH_URL.format(parse.quote(msg), parse.quote(msg))
        # # print(url)
        #
        # response = requests.get(url)
        # response.raise_for_status()
        #
        # print(response.text)
        #
        # soup = bs4.BeautifulSoup(response.text, features='html.parser')
        # # print(soup)

        # 打开浏览器获取指定页面
        a = webbrowser.open("http://www.baidu.com")
        print(a)

    except Exception as e:
        print('there have a error : ' + e)


if __name__ == '__main__':
    baiduSearch('python爬虫')
